import java.util.HashMap;


class P11332_39643 {

    static HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
   
    public static void main(String[] args) {
       
        while (true) {
            int i = Integer.parseInt(readLn());
           
            if (0 == i) break;
           
//            if (i < 10) {
//                System.out.println(i);
//            } else
           
            while (i >= 10) {
//                System.out.println("i = " + i);
                Integer result = map.get(Integer.valueOf(i));
               

//                System.out.println("result = " + result);
               
                if (result != null) {
                    i = result.intValue();
                    break;
                } else {
                    int counter = 0;
                   
                    while (i >= 10) {
                        counter += i % 10;
                        i /= 10;
                       
//                        System.out.println("counter = " + counter);   
//                        System.out.println("i -> " + i);
                    }
                   
                    i = counter + i;
                }
           
           
            }
           
            System.out.println(i);
        }
       
    }
   
    static String readLn() {
        String newLine = System.getProperty("line.separator");
        StringBuilder buffer = new StringBuilder();
        int car = -1;
        try {
            car = System.in.read();
            while ((car > 0) && (car != newLine.charAt(0))) {
                buffer.append((char) car);
                car = System.in.read();
            }
            if (car == newLine.charAt(0))
                System.in.skip(newLine.length() - 1);
        } catch (java.io.IOException e) {
            return (null);
        }
        if ((car < 0) && (buffer.length() == 0))
            return (null);
        return (buffer.toString()).trim();
    }
   
}